/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.AddressEntity;
import entity.ContactEntity;
import entity.PersonEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;
import type.AddressType;

/**
 *
 * @author wei
 */
@Stateless
public class AddressEntityFacade extends AbstractFacade<AddressEntity> {
    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public AddressEntityFacade() {
        super(AddressEntity.class);
    }
    
    public List<AddressEntity> findRange(int[] range, ContactEntity contact) {
        if(contact == null){
            return super.findRange(range);
        }
        
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<AddressEntity> dataObjectRoot = cq.from(AddressEntity.class);
        cq.select(dataObjectRoot);
        cq.where(builder.equal( dataObjectRoot.get("contact"), contact));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        List<AddressEntity> res = new ArrayList<AddressEntity>();
        for(Object obj : q.getResultList()){
            AddressEntity a = (AddressEntity)obj;
            if(a.isArchived() == false)
                res.add(a);
        }
        
        int end = range[1]-range[0]>res.size()?res.size():range[1];
        
        return res.subList(range[0], end);
    }
    
    public List<AddressEntity> getAddressesForContact(ContactEntity contact) {
        if(contact == null){
            return super.findAll();
        }
        
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<AddressEntity> dataObjectRoot = cq.from(AddressEntity.class);
        cq.select(dataObjectRoot);
        cq.where(builder.equal( dataObjectRoot.get("contact"), contact));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        
        return q.getResultList();
    }
    
    public List<AddressEntity> getAddressByTypeForContact(ContactEntity contact, AddressType type){
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<AddressEntity> dataObjectRoot = cq.from(AddressEntity.class);
        cq.select(dataObjectRoot);
        cq.where(builder.and(builder.equal( dataObjectRoot.get("contact"), contact), 
                builder.equal(dataObjectRoot.get("type"), type)));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        
        return q.getResultList();
    }
}
